package assignment;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClient01 {
	protected static final Log logger = LogFactory.getLog(HttpClient01.class);
	public static void main(String[]args) {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		
		HttpGet httpGet = new HttpGet("http://localhost:8801");
		//response model
		CloseableHttpResponse response = null;
		
		try {
			response = httpClient.execute(httpGet);
			//response entity
			HttpEntity entity = response.getEntity();
			logger.info("respose code: "+response.getStatusLine());
			if(null != entity) {
				logger.info("response ContentLength:"+entity.getContentLength());
				logger.info("response Content:"+EntityUtils.toString(entity));
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
				try {
					if(null!=response) {
					response.close();
					}
					if(null != httpClient) {
						httpClient.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		}
	}
}
